package com.snoob.serviceLog.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snoob.serviceLog.log.entity.SysLogEntity;
import com.snoob.serviceLog.log.mapper.SysLogMapper;
import com.snoob.serviceLog.log.service.SysLogService;
import com.snoob.springcloudalibaba.base.BaseException;
import com.snoob.springcloudalibaba.base.BasePageResult;
import com.snoob.springcloudalibaba.pipleline.qo.SelectSysLogQo;
import com.snoob.springcloudalibaba.pipleline.vo.DeleteSysLogVoReq;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2022-05-17
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLogEntity> implements SysLogService {
    /**
     * @描述 添加日志
     */
    @Override
    public Long insertSysLog(SysLogEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        boolean flag = this.save(entity);
        return flag ? entity.getId() : null;
    }

    /**
     * @描述 删除日志
     */
    @Override
    public Boolean deleteSysLog(DeleteSysLogVoReq vo) {
        // 非空校验
        List<String> sysLogIdList = vo.getSysLogIdList();
        if (sysLogIdList.isEmpty()) {
            throw new BaseException("日志编码缺失");
        }
        // 删除条件
        QueryWrapper<SysLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", sysLogIdList);
        boolean deleteSysLogFlag = this.remove(queryWrapper);
        return deleteSysLogFlag;
    }

    /**
     * @描述 查询日志
     */
    @Override
    public SysLogEntity selectSysLog(SelectSysLogQo qo) {
        // 非空校验
        if (null == qo) {
            throw new BaseException("参数缺失");
        }
        // 必填参数校验
        if (null == qo.getId()) {
            throw new BaseException("id缺失");
        }
        SysLogEntity sysLogEntity = this.getById(qo.getId());
        return sysLogEntity;
    }

    /**
     * @描述 分页查询日志
     */
    @Override
    public BasePageResult<SysLogEntity> selectSysLogByPage(SelectSysLogQo qo) {
        // 非空校验
        if (null == qo) {
            throw new BaseException("参数缺失");
        }
        // 查询条件参数
        QueryWrapper<SysLogEntity> entityQueryWrapper = new QueryWrapper<>();
        if (null != qo.getBaseCreateTimeStart()) {
            entityQueryWrapper.ge("create_time", qo.getBaseCreateTimeStart());
        }
        if (null != qo.getBaseCreateTimeEnd()) {
            entityQueryWrapper.le("create_time", qo.getBaseCreateTimeEnd());
        }
        // 分页查询
        Page<SysLogEntity> page = new Page<SysLogEntity>(qo.getBasePageNum(), qo.getBasePageSize());
        page = this.page(page, entityQueryWrapper);
        return new BasePageResult<SysLogEntity>(page);
    }
}
